﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using RMS.Models;

namespace RMS.Controllers
{
    public class UserController : Controller
    {
        RMSDataClassesDataContext db = new RMSDataClassesDataContext();
        [HttpPost]
        public ActionResult ApproveCompany(int userId, int companyId, string passcode)
        {
            Company company = db.Companies.FirstOrDefault(x => x.Id == companyId);
            User user = db.Users.FirstOrDefault(x => x.Id == userId);
            if (company.PassCode == passcode)
            {
                user.CompanyId = companyId;
                db.SubmitChanges();
            }
            return RedirectToAction("SelfDetails");
        }
        public ActionResult SelfDetails()
        {
            ViewData["companies"] = db.Companies.AsEnumerable();
            return View(db.Users.FirstOrDefault(x => x.UserName == HttpContext.User.Identity.Name));
        }
        public ActionResult Details(int id)
        {
            return View();
        }

        //
        // GET: /User/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /User/Create

        [HttpPost]
        public ActionResult Create(FormCollection collection)
        {
            try
            {
                // TODO: Add insert logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
        
        //
        // GET: /User/Edit/5
 
        public ActionResult Edit(int id)
        {
            return View();
        }

        //
        // POST: /User/Edit/5

        [HttpPost]
        public ActionResult Edit(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add update logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        //
        // GET: /User/Delete/5
 
        public ActionResult Delete(int id)
        {
            return View();
        }

        //
        // POST: /User/Delete/5

        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here
 
                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }
    }
}
